package sw6.visualschedule.extendedViews;

import sw6.visualschedule.R;
import sw6.visualschedule.entities.ScheduleActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LinkedActivityView extends LinearLayout {

    private final AnimationListener anilistener1 = new AnimationListener() {
		
		@Override
		public void onAnimationEnd(final Animation animation) {
			startAnimation(mAni2);
		}

		@Override
		public void onAnimationRepeat(final Animation animation) {}

		@Override
		public void onAnimationStart(final Animation animation) {}
	};    
	
    private final AnimationListener anilistener2 = new AnimationListener() {
		
		@Override
		public void onAnimationEnd(final Animation animation) {
			startAnimation(mAni1);
		}

		@Override
		public void onAnimationRepeat(final Animation animation) {}

		@Override
		public void onAnimationStart(final Animation animation) {}
	};
	
    private Animation mAni1;
    private Animation mAni2;
    private Bitmap mBitmap;  
    private Context mContext;
    
    private ImageView mImageView;
    
    private boolean mIsAnimating;
    private LinkedActivityView mLinkedActivityView;
    
    private ScheduleActivity mScheduleActivity;
    
    private TextView mTitleView;
    
    /**
     * Constructs the linked activity view.
     * @param context the context to run in.
     * @param activity the activity related to the view.
     * @param image the bitmap for the activity.
     * @param useText defines if the activity title should be shown.
     */
    public LinkedActivityView(final Context context, final ScheduleActivity activity, final Bitmap image, final Boolean useText) {
    	super(context);
    	mContext = context;
    	mBitmap = image;
    	mImageView = new ImageView(context);
    	mImageView.setImageBitmap(image);
    	
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER);
        setBackgroundDrawable(getResources().getDrawable(R.drawable.my_border));
        addView(mImageView);
        
        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        	     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    	layoutParams.setMargins(30, 20, 30, 0);
        
    	mScheduleActivity = activity;
    	
    	mTitleView = new TextView(context);
    	if (useText) {
            mTitleView.setGravity(Gravity.CENTER);
            mTitleView.setText(mScheduleActivity.getTitle());
            final Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/gilles_handwriting.ttf");        
            mTitleView.setTypeface(font);
            mTitleView.setTextSize(18);
            mTitleView.setTextColor(Color.BLACK);        
            addView(mTitleView);
    	}
    }
    
    /**
     * Constructs the linked activity view.
     * @param context the context to run in.
     * @param activity the activity related to the view.
     * @param image the bitmap for the activity.
     * @param useText defines if the activity title should be shown.
     * @param invisible defines if the image should be invisible.
     */
    public LinkedActivityView(final Context context, final ScheduleActivity activity, final Bitmap image, final Boolean useText, final boolean invisible) {
    	this(context,  activity, image, useText);
    	
    	getImageView().setVisibility(View.INVISIBLE);
    	
    	if (useText) {
    	    mTitleView.setVisibility(View.INVISIBLE);
    	}
    	
    	setBackgroundDrawable(getResources().getDrawable(R.drawable.my_border_transparent));
    }
    
    /**
     * Starts a fade out animation.
     */
    public void fadeOut() {
    	Animation ani = AnimationUtils.loadAnimation(mContext, R.anim.fade_anim);
    	startAnimation(ani);
    }
    
    public Bitmap getBitmap() {
    	return mBitmap;
    }
    
    public ImageView getImageView() {
    	return mImageView;
    }
    
    public LinkedActivityView getLinkedImageView() {
        return mLinkedActivityView;
    }
    
    public ScheduleActivity getScheduleActivity() {
    	return mScheduleActivity;
    }
    
    public boolean isAnimating() {
    	return mIsAnimating;
    }
    
    public void setImageView(final ImageView imageView) {
    	mImageView = imageView;
    }
    
    public void setLinkedImageView(final LinkedActivityView linkedActivityView) {
        mLinkedActivityView = linkedActivityView;
    }
    
    public void startAnimation() {    	
    	if (!mIsAnimating) {    		
    		mIsAnimating = true;
    		mAni1 = AnimationUtils.loadAnimation(mContext, R.anim.activity_anim_1);
	    	mAni1.setAnimationListener(anilistener1);
	    	
	    	mAni2 = AnimationUtils.loadAnimation(mContext, R.anim.activity_anim_2);
	    	mAni2.setAnimationListener(anilistener2);    	
	    	startAnimation(mAni1);
    	}
    }

    /**
     * Stops any running animation.
     */
    public void stopAnimation() {
    	clearAnimation();
    	mIsAnimating = false;
    }
	
    /**
     * Unbinds the drawables. Used to prevent memory leak.
     */
    public void unbindDrawables() {
        if (mImageView.getDrawable() != null) {
            mImageView.getDrawable().setCallback(null);
        }
    }	
}
